using System;
using System.Collections.Generic;
using System.Linq;

namespace KTOP.Core.Domain.Matrix
{
    public class MatrixWithSumms
    {
        private readonly int[][] _matrix;
        private readonly int[] _summs;

        public int Size { get; private set; }

        public IEnumerable<IEnumerable<int>> Matrix { get; set; }
        public IEnumerable<int> Summs { get; set; }

        public int this[int row, int col]
        {
            get
            {
                if (col >= Size) throw new ArgumentOutOfRangeException("col");
                if (row >= Size) throw new ArgumentOutOfRangeException("row");

                return _matrix[row][col];
            }
        }

        protected static int[][] CreateEmptyMatrix(int count)
        {
            return Enumerable.Range(0, count)
                .Select(e => new int[count])
                .ToArray();
        }

        public MatrixWithSumms(int[][] matrix)
        {
            if (matrix.Any(e => e.Length != matrix.Length))
                throw new ArgumentException("matrix");

            _matrix = matrix;
            _summs = matrix.Select(e => e.Sum()).ToArray();

            Size = matrix.Length;
            Matrix = _matrix;
            Summs = _summs;
        }

    }
}